<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;

class BillCategroy extends DbEloquent
{
    use SoftDeletes;
    protected $dates    = ['deleted_at'];
    protected $table    = 'ygt_bill_category';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'pid','title'=>'上级分类','value'=>0,'is_must'=>0,'type'=>1],
        ['name'=>'title','title'=>'分类名称','value'=>'','is_must'=>1],
    ];
    //取系统默认的分类
    public static function getSystemList()
    {
        $where              = ['company_id'=>0];
        $fields             = 'id,company_id,pid,title';
        $result             = self::getList($where,$fields)->toArray();
        return $result;
    }
    //取厂自定义的分类
    public static function getCompanyList($companyId)
    {
        $where              = ['company_id'=>$companyId];
        $fields             = 'id,company_id,pid,title';
        $result             = self::getList($where,$fields)->toArray();
        return $result;
    }
    //取全部分类[包含系统默认和厂自定义的]
    public static function getAllList($companyId,$type=1)
    {
        $systemList         = self::getSystemList();
        $companyList        = self::getCompanyList($companyId);
        $lists              = array_merge($systemList,$companyList);
        $result             = self::changeList($lists,$type);
        return $result;
    }
    //$type 1时一级分类包含二级分类,2时只返回列表不区分一级分类和二级分类
    public static function changeList($lists,$type=1)
    {
        $result         = [];
        switch ($type)
        {
            case 1:
                $parentList     = $listArr = [];
                foreach($lists as $key=>$val)
                {
                    $valId          = $val['id'];
                    $valPid         = $val['pid'];
                    $valTitle       = $val['title'];
                    $valCompanyId   = $val['company_id'];
                    $isSystem       = 0;
                    if($valCompanyId == 0)
                    {
                        $isSystem   = 1;
                    }
                    $temp           = [
                        'id'=>$valId, 'pid'=>$valPid,'title'=>$valTitle,'is_system'=>$isSystem
                    ];
                    if($valPid == 0){
                        $temp['data']      = [];
                        $parentList[]       = $temp;
                    }else{
                        $listArr[$valPid][] = $temp;
                    }
                }
                foreach($parentList as $key=>$val)
                {
                    $valId          = $val['id'];
                    $temp           = $val;
                    if(isset($listArr[$valId]))
                    {
                        $temp['data']   = $listArr[$valId];
                    }
                    $result[]       = $temp;
                }
                break;
            default:
                foreach($lists as $key=>$val)
                {
                    $temp       = ['id'=>$val['id'], 'title'=>$val['title']];
                    $result[]   = $temp;
                }
        }
        return $result;
    }
}
